package com.v5.ik;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Request;
import com.gonghui.videomanage.VideoManage;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zy on 2016/9/26.
 */
@SuppressLint("ValidFragment")
public class PYQFragment extends Fragment implements View.OnClickListener {
    private final String customer;
    private PullToRefreshListView ptr_lv;
    private PYQAdapter pyqAdapter;
    private PYQVideoAdapter pyqVideoAdapter;
    private List<PYQSource> pyqSources = new ArrayList<>();
    private List<PYQSource> pyqSources_video = new ArrayList<>();
    private int pageIndex = 0;
    private int sharetype = 0;//sharetype  0图片   1 视频
    private TextView tv_more, tv_txt, tv_video;

    public PYQFragment(String customer) {
        this.customer = customer;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_pyq, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tv_txt = (TextView) view.findViewById(R.id.tv_txt);
        tv_txt.setOnClickListener(this);
        tv_video = (TextView) view.findViewById(R.id.tv_video);
        tv_video.setOnClickListener(this);
        ptr_lv = (PullToRefreshListView) view.findViewById(R.id.ptr_lv);
        View footView = View.inflate(getContext(), R.layout.more_foot_view, null);
        tv_more = (TextView) footView.findViewById(R.id.tv_more);
        ptr_lv.getRefreshableView().addFooterView(footView);
        pyqAdapter = new PYQAdapter(getActivity(), pyqSources);
        pyqVideoAdapter = new PYQVideoAdapter(getActivity(), pyqSources_video);
        ptr_lv.setAdapter(pyqAdapter);
        ptr_lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                pageIndex = 0;
                getData(Constants.PYQURL + "&PageIndex=" + pageIndex + "&customer=" + customer + "&sharetype=" + sharetype);
            }
        });
        ptr_lv.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                pageIndex++;
                tv_more.setText("加载中...");
                getData(Constants.PYQURL + "&PageIndex=" + pageIndex + "&customer=" + customer + "&sharetype=" + sharetype);
            }
        });
        ptr_lv.setOnScrollListener(VideoManage.getInstance().getOnVideoScrollListener());

        getData(Constants.PYQURL + "&PageIndex=" + pageIndex + "&customer=" + customer + "&sharetype=" + sharetype);
    }

    private void getData(String url) {
        new VolleyHelp() {
            @Override
            public void onJsonData(String s) {
                if (pageIndex == 0) {
                    if (sharetype == 0)
                        pyqSources.clear();
                    else
                        pyqSources_video.clear();
                }

                Gson gson = new Gson();
                try {
                    JSONObject jsonObject = new JSONObject(s);
                    JSONArray items = jsonObject.getJSONArray("Items");
                    for (int i = 0; i < items.length(); i++) {
                        PYQSource pyqSource = gson.fromJson(items.getJSONObject(i).toString(), PYQSource.class);
                        if (sharetype == 0) {
                            pyqSources.add(pyqSource);
                        } else {
                            pyqSources_video.add(pyqSource);
                        }
                    }
                    if (pageIndex == 0) {
                    } else {
                        if (items.length() == 0) {
                            pageIndex--;
                            tv_more.setText("暂无更多");
                        } else {
                            tv_more.setText("加载更多");
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                if (sharetype == 0) {
                    pyqAdapter.notifyDataSetChanged();
                } else {
                    pyqVideoAdapter.notifyDataSetChanged();
                }

                if (ptr_lv.isRefreshing())
                    ptr_lv.onRefreshComplete();
            }
        }.getData(Request.Method.GET, url);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_txt:
                view.setEnabled(false);
                tv_video.setEnabled(true);
                sharetype = 0;
                ptr_lv.setAdapter(pyqAdapter);
                ptr_lv.setRefreshing();
                break;
            case R.id.tv_video:
                view.setEnabled(false);
                tv_txt.setEnabled(true);
                sharetype = 1;
                ptr_lv.setAdapter(pyqVideoAdapter);
                ptr_lv.setRefreshing();
                break;
        }
    }

//    @Override
//    public void onDestroyView() {
//        super.onDestroyView();
//        VideoManage.getInstance().release();
//    }
}
